package com.zzptc.sky.baidudemo.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zzptc.sky.baidudemo.R;
import com.zzptc.sky.baidudemo.base.BaseFragment;
import com.zzptc.sky.baidudemo.services.MyDownLoadService;
import com.zzptc.sky.baidudemo.vo.VersionInfo;

/**
 * Created by Administrator on 2017/5/16.
 */

public class UpdateVersionFragment extends BaseFragment implements View.OnClickListener {
    private TextView tv_update;
    private Button btn_jujue;
    private Button btn_queding;
    private VersionInfo versionInfo;
    @Override
    public int getContentXml() {
        return R.layout.fragment_update_version;
    }

    @Override
    public void initDataView(View view) {
        tv_update = (TextView) view.findViewById(R.id.tv_update);

        btn_jujue = (Button) view.findViewById(R.id.btn_jujue);
        btn_queding = (Button) view.findViewById(R.id.btn_queding);

        btn_queding.setOnClickListener(this);
        btn_jujue.setOnClickListener(this);

        Bundle bundle = getArguments();
        versionInfo = (VersionInfo) bundle.getSerializable("versionInfo");
        if (versionInfo!= null){
            tv_update.setText(versionInfo.getInfo());
            btn_jujue.setVisibility(View.VISIBLE);
        }else {
            tv_update.setText(getMsg());
        }

    }

    public static UpdateVersionFragment getUpdateVersionFragment(VersionInfo versionInfo){
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("versionInfo",versionInfo);
        updateVersionFragment.setArguments(bundle);
        return updateVersionFragment;
    }



    public String getMsg(){
        Bundle bundle = getArguments();
        return (String) bundle.get("msg");
    }
    public static UpdateVersionFragment getUpdateVersionFragment(String string){
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("msg",string);
        updateVersionFragment.setArguments(bundle);
        return updateVersionFragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_jujue:
                dismiss();
                break;
            case R.id.btn_queding:
                if (versionInfo == null){

                }else {
                    Intent intent = new Intent(getActivity(), MyDownLoadService.class);
                    intent.putExtra("apkurl", versionInfo.getApkurl());
                    getActivity().startService(intent);
                }
                dismiss();
                break;
        }
    }
}
